﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 创建者：zhaijc
 * 创建时间：2023/4/14 11:14:47
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

using DotNetty.Transport.Channels.Sockets;
using DotNetty.Transport.Channels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotNetty.Buffers;
using DotNetty.Codecs;

namespace MedianSocketService.netty.MedianProtocol
{
    public class MedianServerInitializer : ChannelInitializer<ISocketChannel>
    {
        public Action<SocketClientInfo> OnConnectedCompletedDelegate;
        public Action<SocketClientInfo, byte[]> OnReceiveCompletedDelegate;
        public Action<SocketClientInfo> OnDisConnectedDelegate;
        protected override void InitChannel(ISocketChannel channel)
        {
            IChannelPipeline pipeline = channel.Pipeline;
            //自定义解码器
            pipeline.AddLast(new MedianProtocolDecoder());
            //请求消息处理类
            MedianProtocolHandler medianProtocolHandler = new MedianProtocolHandler();
            medianProtocolHandler.OnConnectedCompletedDelegate = OnConnectedCompleted;
            medianProtocolHandler.OnDisConnectedDelegate = OnDisConnected;
            medianProtocolHandler.OnReceiveCompletedDelegate = OnReceiveCompleted;
            pipeline.AddLast(medianProtocolHandler);
        }
        public void OnConnectedCompleted(SocketClientInfo sci)
        {
            if (OnConnectedCompletedDelegate != null)
            {
                OnConnectedCompletedDelegate(sci);
            }
        }
        private void OnDisConnected(SocketClientInfo sci)
        {
            if (OnDisConnectedDelegate != null)
            {
                OnDisConnectedDelegate(sci);
            }
        }
        public void OnReceiveCompleted(SocketClientInfo sci, byte[] recvData)
        {
            if (OnReceiveCompletedDelegate != null)
            {
                OnReceiveCompletedDelegate(sci, recvData);
            }
        }
    }
}
